package com.perfma.xtest.request;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileTraversal2 {
    public static void main(String[] args) {
        String folderPath = "/Users/sijunhui/Downloads/log/pid文件/29.16.16.132/resources"; // 指定文件夹路径
        traverseFolder(folderPath);
    }

    public static void traverseFolder(String folderPath) {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            System.out.println("指定的文件夹路径不存在或不是文件夹！");
            return;
        }

        File[] files = folder.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("文件夹为空！");
            return;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                traverseFolder(file.getAbsolutePath()); // 递归遍历子文件夹
            } else {
                readFile(file); // 读取文件内容
            }
        }
    }

    public static void readFile(File file) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String firstLine = reader.readLine(); // 读取第一行数据
            int lineCount = 1;
            while (reader.readLine() != null) {
                lineCount++; // 统计行数
            }
            System.out.println("文件名：" + file.getName());
            System.out.println("第一行数据：" + firstLine);
            System.out.println("行数：" + lineCount);
            System.out.println("--------------------");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
